home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / clantutr.zip / LESSON4 < prev    next >
Text File  |  1986-02-07  |  15KB  |  406 lines

  1. .NT
  2.  A NOTE ABOUT THE LESSONS in C 
  3. .b4-24
  4. .R5C4
  5. These were written while the author was ~Ilearning~N  the language and since
  6. .R6C4
  7. they  are  ~Ifree~N ( to  copy  and/or  distribute ) there  is  a money-back
  8. .R7C4
  9. guarantee on the accuracy of each and every statement in the lessons (!)
  10. .R9C4
  11. The  ~Idisplay~N  program was written ( in C ) in order to provide a vehicle
  12. .R10C4
  13. for displaying the lessons.
  14. .R12C5
  15. .B
  16. P.J.Ponzo
  17. .B
  18. Dept. of Applied Math
  19. .B
  20. Univ. of Waterloo
  21. .B
  22. Ontario N2L 3G1
  23. .K16,30
  24. PonzoTUTOR
  25. .WNT
  26.    FOR WHILE and other good stuff   
  27. .R4C1
  28. ~b~Imain() ~F{~N
  29. ~b~Iint i~N
  30. ~b~Ii=1;~N
  31. ~b~Iwhile (i<11); {~N
  32. ~b~Iprintf("\n The square of %d is %d",i,i*i);~N
  33. ~b~Ii=i+1;~N
  34. ~b~I}~N
  35. ~b~I}~N
  36. .R12C1
  37.     This is the opening ~b~I{~N for ~b~Imain()~N.
  38. .WR11C1
  39. ~b~I~F}~N
  40. .R4C1
  41. ~b~Imain() {~N
  42. .R12C1
  43.     This is the closing ~b~I}~N for ~b~Imain()~N.
  44. .WR11C1
  45. ~b~I}~N
  46. .R5C1
  47. ~Vint i~N
  48. ~Vi=1;~N
  49. .R12C1
  50.     Here we declare ~b~Ii~N to be an ~b~Iint~Neger variable, and define it
  51.     to be (initially) the integer ~b~I1~N.
  52.  
  53.     ~IFind the error here!~N
  54. .WR5C1
  55. ~Vint i~N        should be  ~b~Iint i~F;~N
  56. .R17C1
  57.     WE FORGOT THE SEMI-COLON!
  58. .WR5C1
  59. ~Vint i;~N                                   with SEMI-COLON!
  60. .R12C1
  61.     Now that we're debugging our program, let's change these lines so that
  62.     the declaration and the initialization of ~b~Ii~N are together:
  63.  
  64.                                              
  65.                                              
  66.                                              
  67.                                              
  68.                                              
  69. .WR4C1
  70. ~b~Imain() {~N
  71. ~b~Iint i=1;~N                                                   
  72. ~b~Iwhile (i<11); {~N                                   
  73. ~b~Iprintf("\n The square of %d is %d",i,i*i);~N         
  74. ~b~Ii=i+1;~N                                              
  75. ~b~I}~N                                                    
  76. ~b~I}~N                                                     
  77.           
  78. .K4,60
  79.  int i=1;
  80. .WNR2C1
  81. ~b~Imain() {~N
  82. ~b~Iint i=1;~N                                         
  83. ~Vwhile (i<11); {~N                                   
  84. ~b~Iprintf("\n The square of %d is %d",i,i*i);~N         
  85. ~b~Ii=i+1;~N                                              
  86. ~b~I}~N                                                    
  87. ~b~I}~N                                                     
  88. .R10C1
  89.     Here's something new...it says to execute certain statements again
  90.     and again ~Ias long as i is less than 11~N ( or ~b~Iwhile~N i<11 ).
  91.  
  92.     Execute what statements?
  93. .WR4C1
  94. ~b~Iwhile (i<11); ~F{~N
  95. .R7C1
  96. ~b~I~F}~N
  97. .R15C1
  98.     All the stuff between these curly brackets!
  99.     ...and this stuff says to ~b~Iprintf~N:
  100.  
  101. ~r~I The square of ~N    ~r~Iis~N 
  102.                      
  103.           value of ~b~Ii~N     value of ~b~Ii*i~N (the square of ~b~Ii~N)
  104.           goes in here.        goes in here.
  105. .WR23C1
  106.     THIS PROGRAM IS HARD TO READ!
  107. .K19,60
  108. change it!
  109. .WNT
  110.   PRETTY PROGRAMS  
  111. .R4C1
  112. ~b~Imain()                                              /* sexy program */~N
  113. ~b~I{                                                   /* start main() */~N
  114. ~b~I    int i=1;                                        /* declare i=1  */~N
  115. ~b~I    while (i<11); {                                 /* while i<11   */~N
  116. ~b~I        printf("\n The square of %d is %d",i,i*i);  /* print i, i*i */~N
  117. ~b~I        i=i+1;                                      /* increment i  */~N
  118. ~b~I    }                                               /* end of while */~N
  119. ~b~I}                                                   /* end of main  */~N
  120.  
  121.     Here's the same program again...but nicer to read!
  122.  
  123.     Anything between ~b~I/*~N and ~b~I*/~N is a ~Icomment~N and is ignored
  124.     by the C-compiler (it's for human consumption only) so we've added a
  125.     comment to every line. NOW we can see what the program does by reading
  126.     ~Ionly~N the comments!
  127.  
  128.     Indenting the various parts makes for easier reading (again for human
  129.     consumption ...the  compiler doesn't care).
  130. .WR5C1
  131. ~F~b~I{~N
  132. .R11C1
  133. ~F~b~I}~N
  134. .w
  135. .R13C1
  136.                                                                            
  137.                                                                            
  138.                                                                            
  139.                                                                            
  140.                                                                            
  141.                                                                            
  142.                                                                            
  143.                                                                            
  144.                                                                            
  145. .R13C1
  146.     The start and end of ~b~Imain()~N are easy to spot.
  147.     (Although different programmers use different formats, ~IWE~N will 
  148.      always start and end ~b~Imain()~N with ~b~I{~N and ~b~I}~N in the ~Ifirst~N
  149.      column)    Well ...sometimes we will start with: ~b~Imain() {~N
  150. .WR5C1
  151. ~b~I{~N
  152. .R11C1
  153. ~b~I}~N
  154. .R7C1
  155. ~b~I    while (i<11); ~F{~N~b~I~W                                 /* while i<11   */~N
  156. .R10C1
  157. ~b~I    ~F}~N~b~I~W                                               /* end of while */~N
  158. .R18C1
  159.     ...and we start a ~b~Iwhile~N loop with ~b~Iwhile (....) ~F{~N and end it
  160.     with ~b~I~F}~N placed directly below the ~b~Iw~N in ~b~Iw~Nhile.
  161.  
  162.     ...and we will always (?) ~Iindent~N (by 4 spaces) these ~Iinside loops~N.
  163. .W
  164. .K5,35
  165.  ALWAYS!?
  166. .WNR1C1
  167. ~b~Imain()                                              /* sexy program */~N
  168. ~b~I{                                                   /* start main() */~N
  169. ~b~I    int i=1;                                        /* declare i=1  */~N
  170. ~b~I    while (i<11); {                                 /* while i<11   */~N
  171. ~b~I        printf("\n The square of %d is %d",i,i*i);  /* print i, i*i */~N
  172. ~b~I        i=i+1;                                      /* increment i  */~N
  173. ~b~I    }                                               /* end of while */~N
  174. ~b~I}                                                   /* end of main  */~N
  175.  
  176.     Alas, this program won't even compile!
  177.  
  178.     Whereas most C statements end in a SEMI-COLON, the ~b~Iwhile (...)~N does 
  179.     ~INOT~N. We must delete the ~b~I;~N after a ~b~Iwhile~N.
  180. .R4C1
  181. ~b~I    while (i<11)~F;~N~b~I~W {                                 /* while i<11   */~N
  182. .WR4C1
  183. ~b~I    while (i<11)  {                                 /* while i<11   */~N
  184. .WNR1C1
  185. ~b~Imain()                                              /* sexy program */~N
  186. ~b~I{                                                   /* start main() */~N
  187. ~b~I    int i=1;                                        /* declare i=1  */~N
  188. ~b~I    while (i<11)  {                                 /* while i<11   */~N
  189. ~b~I        printf("\n The square of %d is %d",i,i*i);  /* print i, i*i */~N
  190. ~b~I        i=i+1;                                      /* increment i  */~N
  191. ~b~I    }                                               /* end of while */~N
  192. ~b~I}                                                   /* end of main  */~N
  193.     The construction:   ~V i=1;                     ~N
  194.                         ~V while (i<11)  {          ~N
  195.                         ~V     some statements;     ~N
  196.                         ~V     i=i+1;               ~N
  197.                         ~V }                        ~N
  198.  
  199.     occurs so often (in any language) that a slick mechanism exists ~Ifor~N
  200.     handling this loop:
  201.  
  202.                         ~V for (i=1; i<11; i=i+1) { ~N
  203.                         ~V     some statements;     ~N
  204.                         ~V }                        ~N
  205. .WNR1C1
  206. ~b~Imain()                                              /* sexy program */~N
  207. ~b~I{                                                   /* start main() */~N
  208. ~b~I    int i;                                          /* declare i    */~N
  209. ~b~I    for (i=1; i<11; i=i+1) {                        /* the for loop */~N
  210. ~b~I        printf("\n The square of %d is %d",i,i*i);  /* print i, i*i */~N
  211. ~b~I    }                                               /* end of for   */~N
  212. ~b~I}                                                   /* end of main  */~N
  213.  
  214.     Note that the ~Ifor loop~N automatically initializes ~b~Ii~N to ~b~I1~N,
  215.     then does the ~b~Iprintf()~N again and again, each time incrementing ~b~Ii~N,
  216.     until ~b~Ii~N has the value ~I11~N ( ..then the program exits from this loop
  217.     after ~b~Iprintf~N-ing for the last time with ~b~Ii~N=10). 
  218.  
  219.     The value of ~b~Ii~N, after the exit from the loop, is ~I11~N.
  220. .K19,60
  221.  NOT 10
  222. .WN
  223.     ..and, just to check it all out, we leave our word processor after saving
  224.     this ~Isource~N code under the name ~Iprogram2.c~N, then type:
  225.  
  226. ~Icc program2~N
  227.  
  228.     then (assuming it compiles without errors!) we finish with:
  229.  
  230. ~Ilink program2~N
  231.  
  232.     then ( since this ~Icompile/link~N procedure will generate an ~Iexe~Ncutable
  233.     file called ~Iprogram2.exe~N ) we type:
  234.  
  235. ~Iprogram2~N
  236.  
  237.     and the ~Iexe~Ncutable program will load from disk, then execute, to give:
  238. .K17,30
  239. GO!GO!GO!
  240. .WN
  241.  
  242. ~r~I The square of 1 is 1~N
  243. ~r~I The square of 2 is 4~N
  244. ~r~I The square of 3 is 9~N
  245. ~r~I The square of 4 is 16~N
  246. ~r~I The square of 5 is 25~N
  247. ~r~I The square of 6 is 36~N
  248. ~r~I The square of 7 is 49~N
  249. ~r~I The square of 8 is 64~N
  250. ~r~I The square of 9 is 81~N
  251. ~r~I The square of 10 is 100~N
  252. .K17,30
  253. !
  254. .K17,30
  255. l!
  256. .K17,30
  257. ul!
  258. .K17,30
  259. ful!
  260. .K17,30
  261. rful!
  262. .K17,30
  263. erful!
  264. .K17,30
  265. derful!
  266. .K17,30
  267. nderful!
  268. .K17,30
  269. onderful!
  270. .K17,30
  271. wonderful!
  272. .K17,30
  273. wonderful!
  274. .WN
  275. 1 ~b~I    i=5;                  ~N
  276. 2 ~b~I    while (i<5) {         ~N
  277. 3 ~b~I        some statements;  ~N
  278. 4 ~b~I    }                     ~N
  279.  
  280.     In this piece of code, the ~b~Iwhile~N loop will be executed only as long as
  281.     ~b~Ii<5~N. Since we set ~b~Ii=5~N in Line 1, the loop would be bypassed.
  282.  
  283.     ~IThe condition, in a while loop, is checked at the beginning of the loop!~N
  284.  
  285.     Usually this is what we want .... but, sometimes it is NOT:
  286. .W
  287.  
  288. ~b~I    while (sam>100)    {                              ~N
  289. ~b~I        --------------------------------------------  ~N
  290. ~b~I        some statements which calculate some numbers  ~N
  291. ~b~I        and use these to compute the value of sam.    ~N
  292. ~b~I        --------------------------------------------  ~N
  293. ~b~I    }                                                 ~N
  294.  
  295.     In this piece of code the value of ~b~Isam~N is not even known until we go
  296.     through the ~b~Iwhile~N loop ...so we want to check the ~Iwhile-condition~N
  297.     at the END of the loop !!!
  298. .WNT
  299.     now DO this for a WHILE    
  300. .R4C1
  301. ~b~I    while (sam>100)    {                              ~N
  302. ~b~I        --------------------------------------------  ~N
  303. ~b~I        some statements which calculate some numbers  ~N
  304. ~b~I        and use these to compute the value of sam.    ~N
  305. ~b~I        --------------------------------------------  ~N
  306. ~b~I    }                                                 ~N
  307.  
  308.     We replace the above construction by a ~IDO-WHILE~N:
  309.  
  310. ~b~I    do  {                                             ~N
  311. ~b~I        --------------------------------------------  ~N
  312. ~b~I        some statements which calculate some numbers  ~N
  313. ~b~I        and use these to compute the value of sam.    ~N
  314. ~b~I        --------------------------------------------  ~N
  315. ~b~I    }   while (sam>100)~F;~N~b~I                              ~N
  316.  
  317.     ...and (magic) the ~Iwhile-condition~N is checked at the
  318.     ~Iend of the loop~N!
  319. .K19,60
  320. while;
  321. .WN
  322. 1 ~b~I    double x=1.0, y, e;       /* double precision  ! */ ~N
  323. 2 ~b~I    do  {                     /* start of the do-loop*/ ~N
  324. 3 ~b~I        y=2.0*sin(x);         /* calculate y         */ ~N
  325. 4 ~b~I        e=fabs(y-x);          /* calculate error     */ ~N
  326. 5 ~b~I        x=y;                  /* change x to y       */ ~N
  327. 6 ~b~I    }   while (e>.0000005);   /* end condition       */ ~N
  328. 7 ~b~I    printf("x-2sin(x)=%f when x=%f",e,x);               ~N
  329.  
  330.     This program calulates the root of the equation: ~Ix-2*sin(x)=0~N
  331.     by starting with ~b~Ix=1.0~N (Line 1), then repeatedly replacing ~b~Ix~N
  332.     by y in Line 5 ( where y is calculated as 2.0*sin(x) in Line 3 ).
  333.  
  334.     While the error,( the ~b~If~Nloating point ~b~Iabs~Nolute value of ~b~Iy-x~N,
  335.     calculated in Line 4) exceeds ~b~I.0000005~N we repeat the loop.
  336.  
  337.     Finally, when the ~Iwhile-condition~N (in Line 6) is false (i.e. when 
  338.     ~b~Ie~N is LESS THAN OR EQUAL to .0000005), we print:
  339.  
  340. ~r~Ix-2sin(x)=0.000000 when x=1.895494~N   correct to 6 decimal places!
  341. .W
  342.  
  343.     ..and it's nice to check the error ~b~Ie~N after we go thru' the loop!
  344. .WNT
  345.  a REVIEW 
  346. .R5C1
  347. ~b~Iwhile (something is true ) {~N
  348. ~b~I    do these statements;    ~N
  349. ~b~I}                           ~N
  350.  
  351. ~b~Ifor (initialize variables;repeat,if this is true;do this at end of loop) {~N
  352. ~b~I    do these statements;                                                  ~N
  353. ~b~I}                                                                         ~N
  354.  
  355.     ~INOTE~N: If there is only ~Ione~N statement to perform, in either a ~b~Iwhile~N
  356.             or a ~b~Ifor~N loop, then we don't need the ~b~I{~N and ~b~I}~N:
  357.  
  358. ~b~I                                               ~N 
  359. ~b~Ifor (i=0; i<11; i=i+1)                         ~N     NO OPENING {
  360. ~b~I    printf("\n The square of %d is %d",i,i*i); ~N     or CLOSING }
  361. ~b~I                                               ~N 
  362. .WN
  363. ~b~Ido  {                        ~N
  364. ~b~I    do these statements;     ~N
  365. ~b~I} while (something is true)~F;~N~b~I ~N
  366. .R10C1
  367.         ~INOTE~N: The ~Iwhile~N which occurs at the end of a ~IDO loop~N needs
  368.               a ~ISEMI-COLON~N !!!
  369. .b9-12
  370. .K17,32
  371. but..but..
  372. .WN
  373.     We can also invoke a function ( like ~b~Igetchar()~N ) ~Iwhile~N inside....
  374. ~b~Ichar key;                                ~N
  375. ~b~Iwhile ( (key = getchar()) != 'e' )       ~N
  376. ~b~I    printf(" You pressed %c \n",key);    ~N
  377. ~b~Iprintf("\n  THAT'S THE eND");            ~N
  378.  
  379. ..where we wait for a ~Isingle~N keypress (that's what ~b~Igetchar()~N does!),
  380. and assign the key to the ~b~Ichar~N variable ~b~Ikey~N via ~b~Ikey=getchar()~N,
  381. and so long as ~b~Ikey~N is ~Inot equal~N to the letter ~I'e'~N, we ~b~Iprintf()~N
  382. the ~b~Ikey~N  (as a ~b~I%c~Nharacter) and then a ~b~I\n~Newline .
  383.  
  384. ~INOTE~N: ~b~Iscanf("%c",&key)~N would require your pressing the ~Ienter~N key
  385. after each of the letters ~Ia~N, ~Ib~N, etc.  ... so we used ~b~Igetchar()~N!
  386.  
  387.     This program would give (if you pressed ~Ia~N then ~Ib~N then ~Ic~N etc.):
  388.  
  389. ~Ia ~r You pressed a~N
  390. ~Ib ~r You pressed b~N
  391. ~Ic ~r You pressed c~N
  392. ~Id ~r You pressed d~N
  393. ~Ie~N
  394. ~r~I  THAT'S THE eND~N
  395. .WN
  396.  
  397.  
  398. .T
  399.    THAT'S THE eND FOLKS!   
  400. .K16,30
  401. au revoir!
  402.  
  403.  
  404. .q
  405.  
  406.